package com.leetcode.Offer;

import java.util.ArrayList;
import java.util.List;

public class JZ34 {
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        List<List<Integer>> lists = new ArrayList<>();

        if (root == null) {
            return lists;
        }
        //深度优先遍历
        dfs(root, target, lists, new ArrayList<>());
        return lists;
    }

    private void dfs(TreeNode root, int target, List<List<Integer>> lists, List<Integer> list) {

        int tmp = root.val;
        list.add(tmp);
        if (tmp == target && root.left == null && root.right == null) {//先操作再判断
            lists.add(new ArrayList<>(list));
        }
        if (root.left != null) {
            dfs(root.left, target - tmp, lists, list);
        }
        if (root.right != null) {
            dfs(root.right, target - tmp, lists, list);
        }
        list.remove(list.size() - 1);

    }
}
